<?php
	session_start();

	require_once("includes/connect.php");
	require_once("includes/function.php");

	$panngan2 = $_REQUEST["panngan2"];
	$panngan3 = $_REQUEST["panngan3"];

	$month_year = $_REQUEST["month_year"];

	$budget_year = $_REQUEST["budget_year"];

	$ex1_id = $_REQUEST["ex1_id"];
	$ex2_id = $_REQUEST["ex2_id"];
	$ex3_id = $_REQUEST["ex3_id"];

	$val = $_POST["val"];

	$array1 = $_POST["array1"];

	$pet_id = $_REQUEST["pet_id"];

	$task = $_REQUEST["task"];

	switch($task) {
		case "new":
			$db = NEW myDBs();
			$running = $db->get_running($budget_year);
			

			for($i=0; $i < count($val); $i++) {
				$str_in = $str_in. "'". $val[$i]. "',";
			}
			$str_in = $str_in . "''";
			
			$sql = "SELECT * FROM tbl_init
						WHERE  card_id IN ($str_in)
						AND panngan2_id = '$panngan2'
						AND panngan3_id = '$panngan3'
						AND month_year = '$month_year'
						AND budget_year = '$budget_year'
					";
			
			//echo $sql;
			$db1 = NEW myDBs();
			//$db1->selectDB("account");
			$result = $db->runSQL($sql);

			while ( $rows = $db->fetch_object ( $result )){
				
				$sql = "INSERT INTO tbl_welfare 
				(id, lvl1_id, panngan2_id, panngan3_id, ex1_id, ex2_id, ex3_id, month_year, welfare, pet_id, budget_year)
				VALUES('$rows->card_id', '".$_SESSION["lvl1_id"]. "', '$panngan2', '$panngan3', '$ex1_id', '$ex2_id', '$ex3_id', '$month_year', ";
				
				switch ($ex3_id) {
					 case 23:
						$sql = $sql . "'$rows->rent_home',";
						 break;
					 case 24:
						$sql = $sql . "'$rows->education',";
						 break;
					 case 25:
						 $sql = $sql . "'$rows->medical_fee',";
						 break;
						default:
							$sql = $sql . "'',";
					}
				$sql = $sql ."'$running', '$budget_year')";
				//echo $sql;

				$db1->runSQL($sql);
				
			}

				switch ($ex3_id) {
					 case 23:
						$status = ",rent_home";
						 break;
					 case 24:
						 $status = ",education";
						
						 break;
					 case 25:
						 $status = ",medical_fee";
						 break;
							
					}
				$sql = "UPDATE tbl_init SET status = concat(status,'$status')
						WHERE  card_id IN ($str_in)
						AND panngan2_id = '$panngan2'
						AND panngan3_id = '$panngan3'
						AND month_year = '$month_year'
						AND budget_year = '$budget_year'
					";
				$db1->runSQL($sql);

			echo $running;

			break;
		
		case "edit":

			$db = NEW myDBS();
			$db->selectDB("account");
			$sql = "UPDATE tbl_welfare 
					SET panngan2_id = '$panngan2',
					panngan3_id = '$panngan3',
					ex1_id = '$ex1_id',
					ex2_id = '$ex2_id',
					ex3_id = '$ex3_id',
					month_year = '$month_year',
					budget_year = '$budget_year'
					WHERE pet_id = '$pet_id'
					";
			//echo $array1[1];
			echo $sql;
			$db->runSQL($sql);


			$sql = "select id from tbl_welfare where pet_id = '$pet_id' order by id";
			$result = $db->runSQL($sql);

			$db1 = NEW myDBS;
			$db1->selectDB("account");
			$i=0;
			while ($rows = $db->fetch_object($result)) {
				$sql = "UPDATE tbl_welfare SET welfare = '". $array1[$i] ."'
						WHERE id = '". $rows->id ."' and pet_id = '$pet_id'";
				$db1->runSQL($sql);
				//echo $sql;
				
				/*
				$sql = "UPDATE tbl_init SET "
						
				switch ($ex3_id) {
					case "23":
						$sql = $sql ."rent_home = '". $array1[$i] ."'";
						break;
					case "24":
						$sql = $sql ."education = '". $array1[$i] ."'";
						break;
					case "25":
						$sql = $sql ."medical_fee = '". $array1[$i] ."'";
						break;
				}

				$sql = $sql. " WHERE id = '". $rows->id ."' and month_year = '$month_year'";
				$db1->runSQL($sql);
				$i++;
				*/
			}
			
			break;
		
		case "del":
			$db = NEW myDBS();

			$sql = "DELETE FROM tbl_welfare WHERE pet_id = '$pet_id'";
			$db->runSQL($sql);

			break;
	}
?>
